package com.woniuxy.dr_mall.controller;


import com.woniuxy.dr_mall.entity.*;
import com.woniuxy.dr_mall.global.ResponseStateCode;
import com.woniuxy.dr_mall.service.IDrCommentService;
import com.woniuxy.dr_mall.service.IDrOrdersItemService;
import com.woniuxy.dr_mall.service.IDrPictureService;
import com.woniuxy.dr_mall.service.IDrProductsService;
import com.woniuxy.dr_mall.utils.Audience;
import com.woniuxy.dr_mall.utils.JwtUtil;
import com.woniuxy.dr_mall.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@RestController
@RequestMapping("/dr_mall/drComment")
public class DrCommentController {
    @Autowired
    private IDrCommentService iDrCommentService;
    @Autowired
    private IDrPictureService iDrPictureService;
    @Autowired
    private IDrProductsService iDrProductsService;
    @Autowired
    private IDrOrdersItemService iDrOrdersItemService;
    @Value("${dr.images}")
    private String imagePath;
    @Autowired
    private Audience audience;

    @GetMapping("notCommentList")
    public MyResponseEntity list(HttpSession session,@RequestHeader("token") String token){
        /*DrUser user = (DrUser) session.getAttribute("user");*/
        /*user.getId()*/
        int id= JwtUtil.getId(token,audience.getBase64Secret());

        List<DrCommentInfo> comment = iDrCommentService.getNotComment(id);
        comment.forEach(e->{
            List<String> images = iDrPictureService.getImageByProductId(e.getProductsId());
            e.setImages(images);
            e.setImage("http://localhost:8080/"+images.get(0));

        });

        MyResponseEntity myResponseEntity = new MyResponseEntity();
        myResponseEntity.setCode(ResponseStateCode.SUCCESS);
        myResponseEntity.setData(comment);

        return myResponseEntity;
    }

    @GetMapping("del/{id}")
    public MyResponseEntity del(@PathVariable Integer id){
        iDrCommentService.delById(id);
        return MyResponseEntity.SUCCESS;

    }

    @GetMapping("getComment")
    public MyResponseEntity getComment(HttpSession session,@RequestHeader("token") String token){
        //session 获取用户id
        /*DrUser user = (DrUser) session.getAttribute("user");*/
        int id= JwtUtil.getId(token,audience.getBase64Secret());

        List<DrComment> comments = iDrCommentService.getCommentByUserId(id);
        if (comments != null) {
            comments.forEach(e -> {
                e.setProductName(iDrProductsService.getById(e.getProductId()).getName());
                e.setImages(iDrPictureService.getImageByProductId(e.getProductId()));
                e.setCommentImages(iDrPictureService.getCommentImageByCId(e.getId()));
            });
        }else if (comments == null){
            return MyResponseEntity.NO_DATA;
        }

//      DrUser user = (DrUser) session.getAttribute("user");
        List<DrCommentInfo> list = iDrCommentService.getNotComment(id);
        MyResponseEntity myResponseEntity = new MyResponseEntity();
        myResponseEntity.setCode(ResponseStateCode.SUCCESS);
        myResponseEntity.setData(comments);

        return myResponseEntity;
    }

    @PostMapping("add")
    public MyResponseEntity add(@RequestBody MultipartFile[] file,DrComment comment,HttpSession session,@RequestHeader("token") String token) throws IOException {
        if (file != null) {
            List<String> imageName = new ArrayList<>();
            for (int i = 0; i < file.length; i++) {
                String filename = UUID.randomUUID().toString();
                String suffix = file[i].getOriginalFilename().substring(file[i].getOriginalFilename().lastIndexOf(".") + 1);
                file[i].transferTo(new File(imagePath + "//" + filename + "." + suffix));
                imageName.add(filename + "." + suffix);//imagePath + "//" +
            }
            String time = TimeUtils.getNowTime();
            comment.setTime(time);
            comment.setStatus("y");
            /*userId 此处暂时固定*/
            /*DrUser user = (DrUser) session.getAttribute("user");*/
            int id= JwtUtil.getId(token,audience.getBase64Secret());
            comment.setUserId(id);
            /*增加*/
            iDrCommentService.add(comment);
            //图片
            imageName.forEach(e -> {
                DrPicture drPicture = new DrPicture();
                drPicture.setCommentId(comment.getId());
                drPicture.setImage(e);
                drPicture.setIsDefault("n");
                iDrPictureService.CommentPictureAdd(drPicture);
            });
            iDrOrdersItemService.editStatus(comment.getOrderItemId());

        }else {
            String time = TimeUtils.getNowTime();
            comment.setTime(time);
            comment.setStatus("y");
            /*userId 此处暂时固定*/
            /*DrUser user = (DrUser) session.getAttribute("user");*/
            int id= JwtUtil.getId(token,audience.getBase64Secret());
            comment.setUserId(id);
            System.out.println(comment.toString());
            /*增加*/
            iDrCommentService.add(comment);
            System.out.println(comment.getOrderItemId());
            iDrOrdersItemService.editStatus(comment.getOrderItemId());

        }

        return MyResponseEntity.SUCCESS;
    }

    @GetMapping("getProductComment/{id}")
    public MyResponseEntity getProductComment(@PathVariable int id){
        List<DrComment> comments = iDrCommentService.getProductCommentByPId(id);
        MyResponseEntity myResponseEntity = new MyResponseEntity();
        myResponseEntity.setCode(ResponseStateCode.SUCCESS);
        myResponseEntity.setData(comments);
        return myResponseEntity;

    }
}

